package hang.xipujewelWeb.web.account;

import hang.xipujewelWeb.entity.account.User;
import hang.xipujewelWeb.entity.app.ExchangeRate;
import hang.xipujewelWeb.entity.app.Jewel;
import hang.xipujewelWeb.entity.app.Order;
import hang.xipujewelWeb.entity.app.Ring;
import hang.xipujewelWeb.entity.app.ShopAddress;
import hang.xipujewelWeb.entity.app.ShoppingCart;
import hang.xipujewelWeb.service.account.ExchangeRateManager;
import hang.xipujewelWeb.service.account.OrderManager;
import hang.xipujewelWeb.service.account.ShopAddressManager;
import hang.xipujewelWeb.service.account.ShoppingCartManager;
import hang.xipujewelWeb.service.account.UserManager;
import hang.xipujewelWeb.utils.UserUtil;

import java.util.Date;
import java.util.List;

import javax.servlet.ServletRequest;

import net.esoar.modules.web.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;

@Controller
@RequestMapping("order")
public class OrderController extends BaseController<Order> {

	@Autowired
	private UserManager userManager;

	@Autowired
	private ShoppingCartManager shoppingCartManager;

	@Autowired
	private ExchangeRateManager exchangeRateManager;

	@Autowired
	private ShopAddressManager shopAddressManager;

	@Autowired
	private OrderManager orderManager;

	@RequestMapping("toCommit")
	public String toCommit(Model model, ServletRequest request) {
		User cUser = userManager.findUserByLoginName(UserUtil.getCurrentUserLoginName());
		List<ShoppingCart> shoppingCarts = shoppingCartManager.findByUesr(cUser.getId());
		List<Jewel> jewels = Lists.newArrayList();
		List<Ring> rings = Lists.newArrayList();
		ExchangeRate exchangeRate = exchangeRateManager.getAll().get(0);
		float sum = 0;

		for (ShoppingCart shoppingCartsCart : shoppingCarts) {
			if (shoppingCartsCart.getJewel() != null && shoppingCartsCart.getJewel().getId() != 0) {
				Jewel jewel = shoppingCartsCart.getJewel();
				float discount = (float) ((100.0 + jewel.getDiscount() + cUser.getParent().getPriceBaseNumber()
						.getJewelBaseNumBer()) / 100.0);
				float priceRMB = jewel.getWeight() * jewel.getInternationalPrice() * (discount)
						* exchangeRate.getRate();
				jewel.setSecondPrice(priceRMB);//把算好的价格放在非持久化的字段中，传到前端显示
				jewel.setSecondDiscount((discount - 1) * 100);
				jewel.setShopCartId(shoppingCartsCart.getId());
				jewels.add(jewel);
				sum = sum + jewel.getSecondPrice();
			} else if (shoppingCartsCart.getRing() != null && shoppingCartsCart.getRing().getId() != 0) {
				Ring ring = shoppingCartsCart.getRing();
				if (cUser.getType() == 2) {
					float secondPrice = ring.getPrice() * cUser.getParent().getPriceBaseNumber().getRingBaseNumber();
					ring.setSecondPrice(secondPrice);
					sum = sum + ring.getSecondPrice();
				} else {
					sum = sum + ring.getPrice();
				}
				ring.setShopCartId(shoppingCartsCart.getId());
				rings.add(ring);
			}
		}

		model.addAttribute("orderNum", new Date().getTime());
		model.addAttribute("shopAddress", getShopAddress(cUser));
		model.addAttribute("jewels", jewels);
		model.addAttribute("rings", rings);
		model.addAttribute("count", shoppingCarts.size());
		model.addAttribute("sum", sum);
		return "order/toCommit";
	}

	@RequestMapping(value = "detail", method = RequestMethod.GET)
	public String detail(@RequestParam("id") Long id, Model model) {
		User cUser = userManager.findUserByLoginName(UserUtil.getCurrentUserLoginName());
		Order order = orderManager.get(id);
		List<Jewel> jewels = Lists.newArrayList(order.getJewels());
		List<Ring> rings = Lists.newArrayList(order.getRings());
		ExchangeRate exchangeRate = exchangeRateManager.getAll().get(0);
		float sum = 0;
		int count = 0;
		if (jewels != null) {
			count = count + jewels.size();
			for (Jewel jewel : jewels) {
				float discount = (float) ((100.0 + jewel.getDiscount() + cUser.getParent().getPriceBaseNumber()
						.getJewelBaseNumBer()) / 100.0);
				float priceRMB = jewel.getWeight() * jewel.getInternationalPrice() * (discount)
						* exchangeRate.getRate();
				jewel.setSecondPrice(priceRMB);//把算好的价格放在非持久化的字段中，传到前端显示
				jewel.setSecondDiscount((discount - 1) * 100);
				sum = sum + jewel.getSecondPrice();
			}
			if (rings != null) {
				count = count + rings.size();
				for (Ring ring : rings) {
					if (cUser.getType() == 2) {
						float secondPrice = ring.getPrice()
								* cUser.getParent().getPriceBaseNumber().getRingBaseNumber();
						ring.setSecondPrice(secondPrice);
						sum = sum + ring.getSecondPrice();
					} else {
						sum = sum + ring.getPrice();
					}
				}
			}
		}

		model.addAttribute("orderNum", order.getOrderNum());
		model.addAttribute("jewels", jewels);
		model.addAttribute("rings", rings);
		model.addAttribute("count", count);
		model.addAttribute("sum", sum);

		return "order/orderDetail";
	}

	@RequestMapping(value = "commit/{orderNum}", method = RequestMethod.GET)
	public String commit(@PathVariable("orderNum") String orderNum, Model model) {
		User cUser = userManager.findUserByLoginName(UserUtil.getCurrentUserLoginName());
		List<ShoppingCart> shoppingCarts = shoppingCartManager.findByUesr(cUser.getId());
		String result = shoppingCartManager.commit(shoppingCarts, orderNum, cUser);
		if (result != null && !"".equals(result)) {
			model.addAttribute("hasBuyJewel", result.subSequence(1, result.length() - 1));
		}
		model.addAttribute("orderNum", orderNum);
		return "order/success";
	}

	@RequestMapping(value = "deleteOrder", method = RequestMethod.POST)
	@ResponseBody
	public String deleteOrder(@RequestParam("id") Long id) {
		orderManager.deleteOrder(id);
		return "删除成功";
	}

	private ShopAddress getShopAddress(User user) {
		ShopAddress shopAddress = user.getShopAddress();
		if (shopAddress == null) {
			shopAddress = new ShopAddress();
			shopAddressManager.save(shopAddress);
			user.setShopAddress(shopAddress);
			userManager.save2(user);
		}
		return shopAddress;
	}

	@Override
	public String baseMapping() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getModelName() {
		// TODO Auto-generated method stub
		return null;
	}

}
